<?php
namespace Api\Controller;
use Org\Net\Http;
use Think\Controller;

class FunController extends Controller
{
    protected $connection;
    protected $i=1;
    protected $val='';
    /**************************************************************
     *
     *  使用特定function对数组中所有元素做处理
     *  @param  string  &$array     要处理的字符串
     *  @param  string  $function   要执行的函数
     *  @return boolean $apply_to_keys_also     是否也应用到key上
     *  @access public
     *************************************************************/
    private function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
        static $recursive_counter = 0;
        if (++$recursive_counter > 1000) {
            die('possible deep recursion attack');
        }

        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $this->arrayRecursive($array[$key], $function, $apply_to_keys_also);
            } else {
                $array[$key] = $function($value);
            }

            if ($apply_to_keys_also && is_string($key)) {
                $new_key = $function($key);
                if ($new_key != $key) {
                    $array[$new_key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }
        $recursive_counter--;
    }

    /**************************************************************
     *  将数组转换为JSON字符串（兼容中文）
     *  @param  array   $array      要转换的数组
     *  @return string      转换得到的json字符串
     *  @access public
     *************************************************************/
    public function JSON($array) {
        $this->arrayRecursive($array, 'urlencode', true);
        $json = json_encode($array);
        return urldecode($json);
    }

    /**************************************************************
     *  获取客户端IP
     *  @return string 返回IP
     *  @access public
     *************************************************************/
    public function getIP()
    {
        $ip=getenv('REMOTE_ADDR');
        $ip_ = getenv('HTTP_X_FORWARDED_FOR');
        if (($ip_ != "") && ($ip_ != "unknown"))
        {
            $ip=$ip_;
        }
        return $ip;
    }

    /**
     * 无限级分类
     * @access public
     * @param Array $data //数据库里获取的结果集
     * @param string $idField ID字段名
     * @param string $pidField PID字段名
     * @param string $pid
     * @param Int $count //第几级分类
     * @return Array $treeList
     */
    static public function tree($data, $idField = 'configId', $pidField = 'pid', $pid = '562718dc356e9b4cd03704f4', $count = 1)
    {
        foreach ($data as $key => $value) {

            if ($value[$pidField] == $pid) {
                echo $key,',';
                $value['Count'] = $count;
                $treeList [] = $value;
                unset($data[$key]);
                self::tree($data, $idField, $pidField, $value[$idField], $count + 1);
            }
        }
        return $treeList;
    }


    public function download(){
        header("Content-Type:text/html;charset=UTF-8");
//        $name=$_GET['id'];
        $file_dir = "./Public/Download/speeda5.0.apk";
//        $db=M('medias');
//        $list=$db->where('filename='.$_GET['id'])->select();
        $shwoname="行必达APP";
//        $filename=$file_dir . $name;
        import("@.ORG.Net.Http");
        $http=new Http();
        Http::download($file_dir);

    }
}